'use strict'
const path = require('node:path')
const Package = require('@wjp-cli-dev/manage_package')
module.exports = exec

const settings = {
  'init': '@wjp-cli-dev/init'
}
const CAHE_DIR = 'dependencies'
async function exec () {
  let targetPath = process.env.CLI_TARGET_PATH
  let storePath = ''
  const homePath = process.env.CLI_HOME_PATH
  const cmdObj = arguments[arguments.length - 1]
  const packageName = settings[cmdObj.name()]
  const packageVersion = 'latest'
  let pkg

  // 如果没有传入targetPath
  if (!targetPath) {
    // 获取缓存目录
    targetPath = path.resolve(homePath, CAHE_DIR)
    storePath = path.resolve(targetPath, 'node_modules')
    console.log(targetPath, storePath)
    pkg = new Package({ targetPath, storePath, homePath, packageName, packageVersion })
    if (await pkg.exists()) {
      // 更新package
      console.log('难道')
      // await pkg.install()
    } else {
      // 安装package
      console.log('安装开始！')
      // await pkg.install()
    }
  } else {
    pkg = new Package({ targetPath, homePath, packageName, packageVersion })
  }
  // const rootFile = pkg.getRootFilePath()
  // console.log(rootFile)
  // if (rootFile) {
  //   require(rootFile)(...arguments)
  // }

}
